package project.dade.cn.bst.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;

import butterknife.InjectView;
import project.dade.cn.bst.R;
import project.dade.cn.bst.base.ZJPBaseBarActivity;
import project.dade.cn.bst.utils.ToastUtil;
import project.dade.cn.bst.utils.ValidateUtil;
/**
 * Created by vain zhou on 2016/6/13.
 */
public class AddCaraddtressActivity extends ZJPBaseBarActivity implements View.OnClickListener {
    public static int RESULT_CODE = 1201;
    @InjectView(R.id.et_name)
    EditText name;
    @InjectView(R.id.tv_area)
    EditText tv_eare;
    @InjectView(R.id.et_mobile)
    EditText mobile;
    @InjectView(R.id.et_carnumber)
    EditText carnumber;
    @InjectView(R.id.addBtn)
    AppCompatButton add;
    @InjectView(R.id.universal_toolBar)
    TextView toolbar;
    private String ed_name="";
    private String ed_phone="";
    private String ed_eare;
    private String ed_car;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_caraddress);
        toolbar.setOnClickListener(this);
        add.setOnClickListener(this);
        ed_name=getIntent().getStringExtra("name");
        ed_phone=getIntent().getStringExtra("moblie");
        ed_eare=getIntent().getStringExtra("eare");
        ed_car=getIntent().getStringExtra("carnumber");
        name.setText(ed_name);
        tv_eare.setText(ed_eare);
        mobile.setText(ed_phone);
        carnumber.setText(ed_car);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.addBtn:
                if (isCheckInput()) {
                    Intent intent = new Intent();
                    intent.putExtra("name",name.getText().toString().trim());
                    intent.putExtra("eare",tv_eare.getText().toString().trim());
                    intent.putExtra("moblie",mobile.getText().toString().trim());
                    intent.putExtra("carnumber",carnumber.getText().toString().trim());
                    intent.setClass(AddCaraddtressActivity.this,WashCarSubmitActivity.class);
                    startActivity(intent);
                    finish();
                    }
                break;
            case R.id.universal_toolBar:
                Intent intent = new Intent();
                intent.putExtra("type","1");
                intent.setClass(AddCaraddtressActivity.this,ChooseAddressActivity.class);
                startActivityForResult(intent,  ChooseAddressActivity.RESULT_CODE);
                break;
        }
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode ==ChooseAddressActivity.RESULT_CODE
                && resultCode == ChooseAddressActivity.RESULT_CODE) {
            Bundle mBundle = data.getBundleExtra("tempAddress");
            String street=mBundle.getString("address");
            tv_eare.setText(street);
        }

    }
    private boolean isCheckInput() {
        //姓名不能为空
        if (TextUtils.isEmpty(name.getText().toString().trim())) {
            ToastUtil.showShort(this, R.string.tips_name);
            YoYo.with(Techniques.Shake)
                    .playOn(findViewById(R.id.et_name));
            return false;
        }
        //收获地址
        if (TextUtils.isEmpty(tv_eare.getText().toString().trim())) {
            ToastUtil.showShort(this, R.string.tips_area);
            YoYo.with(Techniques.Shake)
                    .playOn(findViewById(R.id.tv_area));
            return false;
        }
        //验证手机号码
        if (!ValidateUtil.isMobile(mobile.getText().toString().trim())) {
            ToastUtil.showShort(this, R.string.tips_phone_right);
            YoYo.with(Techniques.Shake).playOn(findViewById(R.id.et_mobile));
            return false;
        }
       //验证车牌号
        if (TextUtils.isEmpty(carnumber.getText().toString().trim())) {
            ToastUtil.showShort(this, R.string.tips_name);
            YoYo.with(Techniques.Shake)
                    .playOn(findViewById(R.id.et_name));
            return false;
        }
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            this.finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
